package b1.scenes.run
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.*;
	import b1.scenes.home.*;
	import b1.utils.BFont;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.Mouse;

	public class RunAwayItem extends Sprite
	{
//		private var runawayAnimal:AnimalInfo;
		private var targetPlayerInfo:PlayerInfo;
		private	const animalPosY:int = 95;
		private var animalRunAwayTip:Sprite;
//		private var messageBox:MessageBox;
		private	var bonesForRunAway:int;
//		private var meAnimalInfo:AnimalInfo;
//		private var targetIntelligenceSum:int;

		public function RunAwayItem(targetPlayerInfo:PlayerInfo)
		{
			super();
//			this.runawayAnimal = runawayAnimal;
			this.targetPlayerInfo = targetPlayerInfo;
			//背景框
			var TabItemBackground:Class = ResourceHelper.getInstance().getClassByName("TabItemBackground");
			var tabItemBackground:Sprite = new TabItemBackground(); 
			tabItemBackground.width = 125;
			tabItemBackground.height = 125;
			addChild(tabItemBackground);
			//家背景
			tabItemBackground = new TabItemBackground();
			tabItemBackground.width = 125;
			tabItemBackground.height = 125;
			addChild(tabItemBackground);
			var HomeBackGround:Class = ResourceHelper.getInstance().getClassByName("Home" + targetPlayerInfo.homeIndex);
			var homeImage:Sprite = new HomeBackGround();
			homeImage.width = tabItemBackground.width;
			homeImage.height = tabItemBackground.height;
			homeImage.alpha = 0.5;
			homeImage.mask = tabItemBackground;
			addChild(homeImage);
			//动物
			var animalMc:AnimalMc;
			if (targetPlayerInfo.liveAnimals.length == 1)
			{
				animalMc = new AnimalMc(targetPlayerInfo.liveAnimals[0], 75);
				animalMc.x = this.width / 2;
				animalMc.y = animalPosY;
				addChild(animalMc);
			}
			else if (targetPlayerInfo.liveAnimals.length > 1)
			{
				for (var i:int = 0; i < 2; i++)
				{
					animalMc = new AnimalMc(targetPlayerInfo.liveAnimals[i], 45);
					animalMc.x = this.width * 5 / 16 + this.width * i * 6 / 16;
					animalMc.y = animalPosY;
					addChild(animalMc);
				}
			}
			//标题名字
			var titleText:TextField = new TextField();
			titleText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.LEFT;
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			titleText.text = targetPlayerInfo.name.substr(0,4);
			titleText.x = (tabItemBackground.width - titleText.width) / 2;
			titleText.y = 95;
			addChild(titleText);
			addEventListener(MouseEvent.CLICK, onRunAwayItemClick);
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			this.filters = [new ColorMatrixFilter(
				[1,0,0,0,15,
			     0,1,0,0,15,
			     0,0,1,0,15,
			     0,0,0,1,0 ])]; 
			animalRunAwayTip = new Sprite();
			var roundRec:RoundRec = new RoundRec(150, 180, 0.8, BFont.roundRecColor);
			animalRunAwayTip.addChild(roundRec);
				
			//“单击跑路”
			var helpText:TextField = new TextField();
			helpText.selectable = false;
        	helpText.width = roundRec.width;
        	helpText.autoSize = TextFieldAutoSize.CENTER;
			var helpTexFormat:TextFormat = new TextFormat("微软雅黑", 15, 0x000000, true);
			helpTexFormat.leftMargin = 5;
			helpTexFormat.rightMargin = 5;
			helpTexFormat.color = 0x008000;
			helpText.defaultTextFormat = helpTexFormat;
			helpText.text = "单击跑路";
			helpText.y = 10;
			animalRunAwayTip.addChild(helpText);
			
			
			var gap:int = 10;
			//名字
			var nameText:TextField = new TextField();
			nameText.selectable = false;
        	nameText.width = roundRec.width;
        	nameText.autoSize = TextFieldAutoSize.LEFT;
			var nameTexFormat:TextFormat = new TextFormat("微软雅黑", 15, 0x000000, true);
			nameTexFormat.leftMargin = 5;
			nameTexFormat.rightMargin = 5;
			nameText.defaultTextFormat = nameTexFormat;
			nameText.y = 30;
			nameText.x = gap;
			nameText.text = targetPlayerInfo.name.substr(0,4);
			animalRunAwayTip.addChild(nameText);
			
			//传家宝
			var heirloomsText:TextField = new TextField();
			heirloomsText.selectable = false;
			heirloomsText.autoSize = TextFieldAutoSize.LEFT;
			var heirloomsTextFormat:TextFormat = new TextFormat("微软雅黑",13,0x000000);
			heirloomsTextFormat.leftMargin = 5;
			heirloomsTextFormat.rightMargin = 5;
			heirloomsText.defaultTextFormat = heirloomsTextFormat;
			var heirloomsNum:int  = 0;
			for(var index:String in this.targetPlayerInfo.heirlooms)
			{
				heirloomsNum++;
			}
			heirloomsText.text = ("拥有传家宝: " + heirloomsNum);
			heirloomsText.y = 50;
			heirloomsText.x = gap;
			animalRunAwayTip.addChild(heirloomsText);
			
			//居住的动物
			var liveAnimalNames:TextField = new TextField();
			liveAnimalNames.selectable = false;
			liveAnimalNames.autoSize = TextFieldAutoSize.LEFT;
			var liveAnimalNamesTextFormat:TextFormat = new TextFormat("微软雅黑", 13, 0x000000);
			liveAnimalNamesTextFormat.leftMargin = 5;
			liveAnimalNamesTextFormat.rightMargin = 5;
			liveAnimalNames.defaultTextFormat = liveAnimalNamesTextFormat;
			liveAnimalNames.appendText( "居住动物: " + targetPlayerInfo.liveAnimals.length);
			liveAnimalNames.y = 70;
			liveAnimalNames.x = gap;
			animalRunAwayTip.addChild(liveAnimalNames);
			var animalInfo:AnimalInfo = PlayerInfoHelper.getInstance().animals[this.targetPlayerInfo.liveAnimals[0]];
			var animalInfoText:TextField = new TextField();
			animalInfoText.selectable = false;
			animalInfoText.autoSize = TextFieldAutoSize.LEFT;
			animalInfoText.defaultTextFormat = liveAnimalNamesTextFormat;
			if (null != animalInfo)
			{
				animalInfoText.appendText("等级: " + animalInfo.level +"\n" + "血量: " + animalInfo.currentHealthPoint);
				animalInfoText.appendText("\n体力: " + animalInfo.currentVitalityPoint + "\n" + "速度: " + animalInfo.speed);
			}
			animalInfoText.y = 90;
			animalInfoText.x = gap;
			animalRunAwayTip.addChild(animalInfoText);
						
			//加上提示的位置
			if (this.x < 350)
			{
				animalRunAwayTip.x = this.x + 280;
			}
			else
			{
				animalRunAwayTip.x = this.x + 7;
			}
			animalRunAwayTip.y = this.y + 105;
			SceneContainer.getInstance().currentScene.addChild(animalRunAwayTip);
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			this.filters = null;
			if (null != animalRunAwayTip && null != animalRunAwayTip.parent)
			{
				animalRunAwayTip.parent.removeChild(animalRunAwayTip);
			}
		}
		
		private function onRunAwayItemClick(event:MouseEvent):void
		{
			//跑路的代价
			var targetIntelligenceSum:int = 0;
			for (var i:int = 0; i < this.targetPlayerInfo.liveAnimals.length; i++)
			{
				var liveAnimalInfo:AnimalInfo = PlayerInfoHelper.getInstance().animals[this.targetPlayerInfo.liveAnimals[i]];
				targetIntelligenceSum += liveAnimalInfo.intelligence;
			}
			var me:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];	
			var myAnimalInfo:AnimalInfo = PlayerInfoHelper.getInstance().animals[me.ownAnimals[0]];	
			var intelligenceBalance:int = targetIntelligenceSum - myAnimalInfo.intelligence;
			bonesForRunAway = Math.max(intelligenceBalance * Config.bonePerIntelligenceForRunAway, Config.baseBoneForRunAway);
			var message:String = "你和驻守该家的动物智力比拼" + myAnimalInfo.intelligence + "：" + targetIntelligenceSum + "\n需要缴纳骨头: " + bonesForRunAway + "，是否继续？";
			var messageBox:MessageBox = new MessageBox(message, 1, true);
			messageBox.x = (BFont.stageWidth - messageBox.width) / 2;
			messageBox.y = (BFont.stageHeight - messageBox.height) / 2;
			SceneContainer.getInstance().currentScene.addChild(messageBox);
			messageBox.buttonOK.addEventListener(MouseEvent.CLICK, onButtonOKClick);
		}
		
		private function onButtonOKClick(event:MouseEvent):void
		{
			var runawayAnimal:AnimalInfo = RunAwayDialog.getInstance().runawayAnimal;
			ServerHelper.getInstance().runAwayAnimal(runawayAnimal.id, targetPlayerInfo.id, bonesForRunAway, onRunAwayComplete);
		}

		private function onRunAwayComplete(result:String):void
		{
			if (result == "success")
			{
				RunAwayDialog.getInstance().close();
				var home:Home = SceneContainer.getInstance().currentScene as Home;
				home.switchLiveId(targetPlayerInfo.id);
			}
		}
	}
}